﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using QLVT.Config;
using QLVT.Entity;

namespace QLVT.DAO
{
    class PhieuNhapDAO:DAOBase
    {
        public DataSet GetAll()
        {
            return Select("select * from PhieuNhap");
        }

        public PhieuNhap GetById(String sophieuid)
        {
            PhieuNhap phieunhap = null;
            SqlConnection conn = null;
            try
            {
                conn = Config.Config.getConnection();
                conn.Open();
                SqlCommand commnad = new SqlCommand("select * from PhieuNhap where sophieu = '" + sophieuid + "'", conn);
                SqlDataReader dataReader = commnad.ExecuteReader();
                if (dataReader.Read())
                {
                    phieunhap = new PhieuNhap();
                    phieunhap.Sophieu = dataReader.GetString(0);
                    phieunhap.Ngaynhap = dataReader.GetDateTime(1);
                    phieunhap.Manv = dataReader.GetString(2);
                    return phieunhap;
                }
            }
            catch (Exception ex)
            {

            }
            finally
            {
                conn.Close();
                
            }
            return phieunhap;
        }

        public bool DeleteById(String sophieu)
        {
            return Update("delete from PhieuNhap where sophieu = '" + sophieu + "'");
        }

        public bool UpdateById(PhieuNhap phieunhap)
        {
            String sql = "update PhieuNhap "
                 + "set ngaynhap = '" + phieunhap.Ngaynhap + "'"
                 + ", manv = '" + phieunhap.Manv + "'"
                 + " where sophieu = " + phieunhap.Sophieu + "'";

            return Update(sql);
        }
    }
}
